#include <iostream>
#include <cmath>
#include "Spline.h"
#include "iofile.h"
class F1 :public Function{
public:
    double operator()(const double &x)const {
        return 1/(1+x*x);
    }

    double diff(const double &x) const{
        return (-2*x)/(1+x*x)/(1+x*x);
    }
};

F1 f1;

void test1(){
    vector<double> t={-5,-4,-3,-2,-1,0,1,2,3,4,5};
    vector<double> ft;
    for(auto&x :t){
        if(x == t.front()) {
            ft.push_back(f1(x));
            ft.push_back(f1(x+0.5));
        }
        else if(x == t.back()){
            ft.push_back(f1(x));
        }
        else ft.push_back(f1(x+0.5));
    }


    BSpline_3 b3(f1,t,complete);
    BSpline_2 b2(t,ft);

    vector<double> test_t={-3.5,-3,-0.5,0.0,0.5,3,3.5};
    vector<double> test_bspline_3;
    vector<double> test_bspline_2;
    for(auto&x:test_t){
        test_bspline_2.push_back(abs(f1(x) - b2(x)));
        test_bspline_3.push_back(abs(f1(x) - b3(x)));
    }

    cout<<"error of cardinal quadratic:"<<endl;

    for(int i=0;i<test_t.size();i++){
        cout <<"x = "<< test_t[i]<<" : error = "<<test_bspline_2[i]<<endl;
    }
    cout <<" "<<endl;

    cout<<"error of cardinal cubic:"<<endl;

     for(int i=0;i<test_t.size();i++){
        cout <<"x = "<< test_t[i]<<" : error = "<<test_bspline_3[i]<<endl;
    }
    cout <<" "<<endl;



    vector<double> xx;
    vector<double> y1;
    vector<double> y2;
    int fitnum=500;
    for(int i=0;i<fitnum;i++){
        xx.push_back(-5+10.0*i/float(fitnum));
        y1.push_back(b3(-5+10.0*i/float(fitnum)));
        y2.push_back(b2(-5+10.0*i/float(fitnum)));
    }

    ClearFile("C.txt");
    VectorToFile(xx,"C.txt");
    VectorToFile(y1,"C.txt");
    VectorToFile(y2,"C.txt");
}


int main()
{
    test1();
}
